package com.casic.modules.ddqiye.controller;

import com.casic.common.annotation.SysLog;
import com.casic.common.base.entity.PageEntity;
import com.casic.common.utils.CommonUtil;
import com.casic.common.utils.Constant;
import com.casic.common.utils.R;
import com.casic.common.validator.group.UpdateGroup;
import com.casic.modules.ddqiye.entity.QyxxDdEntity;
import com.casic.modules.ddqiye.service.QyxxDdService;
import com.casic.modules.sys.entity.SysDeptEntity;
import com.casic.modules.sys.entity.SysUserEntity;
import com.casic.modules.sys.service.SysDeptService;
import com.casic.modules.sys.service.SysUserRoleService;
import com.casic.modules.sys.service.SysUserService;
import com.casic.modules.sys.shiro.ShiroUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.Errors;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.UUID;


/**
 * 典当企业信息表
 * 
 * @author ws
 * @email 123456789@qq.com
 * @date 2019-05-13 11:25:52
 */
@RestController
@RequestMapping("qyxxdd")
public class QyxxDdController {
	@Autowired
	private QyxxDdService qyxxDdService;
	@Autowired
	private SysDeptService sysDeptService;
	@Autowired
	private SysUserService sysUserService;
	@Autowired
	private SysUserRoleService sysUserRoleService;
	/**
	 * 列表
	 */
	@RequestMapping("/list")
	@RequiresPermissions("qyxxdd:list")
	public R list(@RequestParam Map<String, Object> params){
		List<String> roles = sysUserRoleService.findRoleIdByUserId(ShiroUtils.getUserEntity().getUserId());
		if(roles.size() == 1 && Constant.DDQY_ROLE_ID.equals(roles.get(0))) {
			params.put("ddQydm", ShiroUtils.getUserEntity().getUsername());
		}
		if(CommonUtil.isGongan(roles)) {
			params.put("deptId", ShiroUtils.getUserEntity().getDeptId());
		}
        PageEntity<QyxxDdEntity> page=qyxxDdService.findPage(params);
		return R.ok().put("page", page);
	}
	
	
	/**
	 * 信息
	 */
	@RequestMapping("/info/{id}")
	@RequiresPermissions("qyxxdd:info")
	public R info(@PathVariable("id") String id){
		QyxxDdEntity qyxxDd = qyxxDdService.findById(id);
		return R.ok().put("qyxxDd", qyxxDd);
	}
	
	/**
	 * 保存
	 */
	@RequestMapping("/save")
	@RequiresPermissions("qyxxdd:save")
	public R save(@RequestBody QyxxDdEntity qyxxDd){
		// 检查企业代码是否存在，企业代码也是用户名，不允许重复。
		QyxxDdEntity qyxxDdEntity = qyxxDdService.findDdqyByQydm(qyxxDd.getDdQydm());
		if(qyxxDdEntity != null) {
			String msg = "企业代码：" + qyxxDdEntity.getDdQydm() + "已存在！";
			return R.error(400, msg);
		}

		// 设置所属分局
		qyxxDd.setDdSsfj(queryParentId(qyxxDd));
		qyxxDdService.save(qyxxDd);

		// 新增典当企业同时添加用户，角色典当企业管理员，初始密码123456
		SysUserEntity sysUserEntity = new SysUserEntity();
		sysUserEntity.setUsername(qyxxDd.getDdQydm());
		sysUserEntity.setNickName(qyxxDd.getDdQymc());
		sysUserEntity.setPassword("123456");
		sysUserEntity.setSalt(UUID.randomUUID().toString());
		sysUserEntity.setDeptId(qyxxDd.getDdSspcs());
		List<String> roleIdList = new ArrayList<>();
		roleIdList.add("9c9f8a5d80f9469ca18483c72d6b9971");
		sysUserEntity.setRoleIdList(roleIdList);
		List<String> deptIdList = new ArrayList<>();
		deptIdList.add(qyxxDd.getDdSspcs());
		sysUserEntity.setDeptIdList(deptIdList);
		sysUserEntity.setStatus(1);
		sysUserService.save(sysUserEntity);

		return R.ok();
	}
	
	/**
	 * 修改
	 */
	@RequestMapping("/update")
	@RequiresPermissions("qyxxdd:update")
	public R update(@RequestBody QyxxDdEntity qyxxDd){
		qyxxDdService.edit(qyxxDd);
		return R.ok();
	}
	
	/**
	 * 删除
	 */
	@SysLog("删除企业同时删除企业用户")
	@RequestMapping("/delete")
	@RequiresPermissions("qyxxdd:delete")
	public R delete(@RequestBody String[] ids){
		String[] userId = qyxxDdService.findUserIdsByQyIds(ids);
		qyxxDdService.removeByIds(ids);
		sysUserService.deleteBatch(userId);
		return R.ok();
	}

	/**
	 * 根据派出所机构号查询所属分局机构号
	 * @param qyxxDd 典当企业信息
	 * @return 所属分局机构号，如果无记录或为根节点则返回传入的机构号
	 */
	private String queryParentId(QyxxDdEntity qyxxDd) {
		// 根据派出所机构号查询所属分局机构号
		SysDeptEntity sysDeptEntity = sysDeptService.queryObject(qyxxDd.getDdSspcs());
		String ssfj = null;
		if(sysDeptEntity != null) {
			ssfj = sysDeptEntity.getParentId();
		}
		if(ssfj == null || "".equals(ssfj)) {
			ssfj = qyxxDd.getDdSspcs();
		}
		return ssfj;
	}
}
